CLASS ymoon_test_class DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.

    "상수
    CONSTANTS:
      content_type TYPE string VALUE 'Content-type',
      json_content TYPE string VALUE 'application/json; charset=UTF-8'.

  PROTECTED SECTION.
  PRIVATE SECTION.
    METHODS: create_so IMPORTING out TYPE REF TO if_oo_adt_classrun_out,
      call_api IMPORTING out TYPE REF TO if_oo_adt_classrun_out,
      test_xml IMPORTING out TYPE REF TO if_oo_adt_classrun_out.

ENDCLASS.



CLASS YMOON_TEST_CLASS IMPLEMENTATION.


  METHOD call_api.
    DATA:http_client TYPE REF TO ymoon_api.
    CREATE OBJECT http_client
      EXPORTING
        i_scenario     = 'YMOON_CS_API012'
        i_service      = 'YMOON_OS_API012_REST'
      EXCEPTIONS
        no_arrangement = 1.
    IF sy-subrc = 1.
      out->write( 'no arrangement' ).
    ENDIF.
    CHECK sy-subrc <> 1.

    DATA param TYPE string.
    DATA res TYPE string.
    param = 'crkyCn=u270f148l018m219a020m040c0&qryYymmDd=20150101&imexTp=2'.

    res = http_client->get( param ).


    DATA: lv_xstring TYPE xstring.
    DATA: lv_conv TYPE REF TO cl_abap_conv_codepage.
    lv_xstring = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( source = res ).
    out->write( lv_xstring ).

    DATA:BEGIN OF ls_data,
           cntysgn   TYPE c  LENGTH 2,
           mtryutnm  TYPE c  LENGTH 30,
           fxrt      TYPE c  LENGTH 13,
           currsgn   TYPE c  LENGTH 3,
           aplybgndt TYPE c  LENGTH 8,
           imextp    TYPE c  LENGTH 1,
         END OF ls_data.
    DATA:lt_data LIKE TABLE OF ls_data.

    DATA:gv_cond TYPE c LENGTH 50 VALUE 'trifFxrtInfoQryRsltVo'.
    DATA:gv_field TYPE c LENGTH 20.
    FIELD-SYMBOLS <fs> TYPE any.


    DATA: lv_reader          TYPE REF TO if_sxml_reader.
    DATA: lv_node            TYPE REF TO if_sxml_node.
    DATA: lv_open_node       TYPE REF TO if_sxml_open_element.
    DATA: lv_close_node      TYPE REF TO if_sxml_close_element.
    DATA: lv_value_node      TYPE REF TO if_sxml_value.
    DATA: lv_value           TYPE string.

    lv_reader = cl_sxml_string_reader=>create( lv_xstring ).

    lv_node = lv_reader->read_next_node( ).


    ASSERT lv_node IS NOT INITIAL.
    WHILE lv_reader->node_type <> if_sxml_node=>co_nt_final.
      CASE lv_reader->node_type.
        WHEN if_sxml_node=>co_nt_element_open.
          lv_open_node  ?= lv_node.

          gv_field = 'ls_data-' && lv_open_node->qname-name.

        WHEN if_sxml_node=>co_nt_element_close.
          lv_close_node ?= lv_node.

          IF lv_close_node->qname-name = gv_cond.
            APPEND ls_data TO lt_data.
            CLEAR ls_data.
          ELSE.
            ASSIGN (gv_field) TO <fs>.
            IF sy-subrc = 0.
              <fs> = lv_value.
            ENDIF.
          ENDIF.

        WHEN if_sxml_node=>co_nt_value.
          lv_value_node ?= lv_node.
          lv_value = lv_value_node->get_value( ).
        WHEN if_sxml_node=>co_nt_attribute.
      ENDCASE.
      lv_node = lv_reader->read_next_node( ).
    ENDWHILE.

    LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<fs_line>).

    ENDLOOP.


  ENDMETHOD.


  METHOD create_so .


    DATA: lr_cscn TYPE if_com_scenario_factory=>ty_query-cscn_id_range.
    lr_cscn = VALUE #( ( sign = 'I' option = 'EQ' low = 'YMOON_CS_VBAK' ) ).

    DATA(lo_factory) = cl_com_arrangement_factory=>create_instance( ).
    lo_factory->query_ca(
          EXPORTING
            is_query           = VALUE #( cscn_id_range = lr_cscn )
          IMPORTING
            et_com_arrangement = DATA(lt_ca)
        ).

    DATA(lo_ca) = lt_ca[ 1 ].

    "Item Body
    DATA : item_json TYPE string.

    IF ( item_json <> '' ).
      item_json = item_json && ',{'.
    ENDIF.

    item_json = item_json && '"SalesOrderItem":"' && '10' && '",' &&
                             '"Material":"' && 'CN001' && '",' &&
                             '"RequestedQuantity":"' && '1' && '",' &&
                             '"RequestedQuantityUnit":"' && 'EA' && '"' &&
                             '}'.


    "Partner Body
    DATA : partner_json TYPE string.

    IF ( partner_json <> '' ).
      partner_json = partner_json && ',{'.
    ENDIF.

    partner_json = partner_json && '"PartnerFunction":"' && 'SH' && '",' &&
                                   '"Customer":"' && '1000031' && '"' &&
                                   '}'.




    "Pricing Body
    DATA : pricing_json TYPE string.
    pricing_json = pricing_json && '"ConditionType":"' && '' && '",' &&
                                   '"ConditionRateValue":"' && '0' && '",' &&
                                   '"ConditionCurrency":"' && '' && '"' &&
                                   '}'.



    " post할 데이터 가공
    DATA(json) =
        '{' &&
        ' "SenderBusinessSystemName":"' && 'ASPNCHINA' && '",' &&
        ' "PurchaseOrderByCustomer":"' && 'POCN0001' && '",' &&
        ' "SalesOrderType":"' && 'OR' && '",' &&
        ' "SalesOrganization":"' && '4310' && '",' &&
        ' "DistributionChannel":"' && '10' && '",' &&
        ' "SoldToParty":"' && '1000099' && '",' &&
        "' "RequestedDeliveryDate":"' && DeliveryYear && '-' && Deliverymonth && '-' && Deliveryday && '",' && "오류용 주석
        ' "RequestedDeliveryDate":"' && '2023-11-30T00:00:00' && '",' &&
        ' "CustomerPurchaseOrderDate":"' && '2023-11-23T00:00:00' && '",' &&
        ' "SalesOrderDate":"' && '2023-11-23T00:00:00'  && '",' &&
        ' "to_Item":{' &&
        '  "results":[{' &&
        '  ' && item_json &&
        '  ]' &&
        ' },' &&
        ' "to_Partner":{' &&
        '  "results":[{' &&
        '  ' && partner_json &&
        '  ]' &&
        ' },' &&
        ' "to_PricingElement":{' &&
        '  "results":[{' &&
        '  ' && pricing_json &&
        '  ]' &&
        ' }' &&
        '}'.

    "GET
    TRY.
        DATA(lo_dest) = cl_http_destination_provider=>create_by_comm_arrangement(
            comm_scenario  = 'YMOON_CS_VBAK'
            service_id     = 'YMOON_OS_SALESORDER_REST'
            comm_system_id = lo_ca->get_comm_system_id( ) ).
        DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_dest ).

        DATA(lo_request) = lo_http_client->get_http_request( ).
        lo_request->set_uri_path( EXPORTING i_uri_path = '?$top=1' ).
        lo_request->set_header_field( i_name = 'x-csrf-token' i_value = 'fetch' ).
        DATA(lo_response) = lo_http_client->execute( if_web_http_client=>get ).

        "get 해서, token이랑 cookie값 가져오기
        DATA(token)   = lo_response->get_header_field( i_name = 'x-csrf-token' ).
        DATA(cookies) = lo_response->get_cookies( ).

      CATCH cx_http_dest_provider_error.
        " handle exception here

      CATCH cx_web_http_client_error.
        " handle exception here
    ENDTRY.

    "POST
    TRY.
        lo_dest = cl_http_destination_provider=>create_by_comm_arrangement(
            comm_scenario  = 'YMOON_CS_VBAK'
            service_id     = 'YMOON_OS_SALESORDER_REST'
            comm_system_id = lo_ca->get_comm_system_id( ) ).
        lo_http_client = cl_web_http_client_manager=>create_by_http_destination( lo_dest ).
        lo_request  = lo_http_client->get_http_request( ).

        "json body 설정
        lo_request->set_text( json ).
        "GET에서 가져왔던 cookie, token값 셋팅
        LOOP AT cookies INTO DATA(cookie).
          lo_request->set_cookie( i_name = cookie-name i_value = cookie-value ).
        ENDLOOP.

        lo_request->set_header_field( i_name = content_type   i_value = json_content ).
        lo_request->set_header_field( i_name = 'Accept' i_value = 'application/json' ).
        lo_request->set_header_field( i_name = 'x-csrf-token' i_value = token ).
        lo_response = lo_http_client->execute( if_web_http_client=>post ).

        DATA(body)   = lo_response->get_text( ).
        DATA(status) = lo_response->get_status( )-code.

        out->write( body ).

      CATCH cx_http_dest_provider_error.
        " handle exception here

      CATCH cx_web_http_client_error.
        " handle exception here
    ENDTRY.
  ENDMETHOD.


  METHOD if_oo_adt_classrun~main.
    call_api( out ).
  ENDMETHOD.


  METHOD test_xml.
    DATA: lv_xstring         TYPE xstring.
    DATA: lv_xml TYPE string.

    DATA: lv_reader          TYPE REF TO if_sxml_reader.
    DATA: lv_node            TYPE REF TO if_sxml_node.
    DATA: lv_open_node       TYPE REF TO if_sxml_open_element.
    DATA: lv_close_node      TYPE REF TO if_sxml_close_element.
    DATA: lv_value_node      TYPE REF TO if_sxml_value.
    DATA: lv_value           TYPE string.

    DATA: lv_conv TYPE REF TO cl_abap_conv_codepage.


    lv_xml = `<grid gridtype="16Column2Panel" rows="2 " cols="16 " width="100%">`
             && `<gridcell col="1" row="1" colspan="8">`
             &&  `<header id="FA163EEF573D1ED3BDD89BC23ABF16C8" textid="FA163EEF573D1ED3BDD89BC23ABF16C8"/>`
             && `</gridcell>`
             && `<gridcell col="1" row="2" colspan="3">`
              && `<label for="//ROLE/VALUE"/>`
             && `</gridcell>`
             && `<gridcell col="4" row="2" colspan="5">`
              && `<picklist id="//ROLE/VALUE" table="//ROLE/VALUE" selection="//ROLE/VALUE" mandatory="" disabled=""/>`
             && `</gridcell>`
            && `</grid>`.

     data(lo_xml) = new cl_ixml_core( ).
*    lv_xstring = cl_abap_conv_codepage=>create_out( codepage = `UTF-16LE` )->convert( source = lv_xml ).
*
*
*    lv_reader = cl_sxml_string_reader=>create( lv_xstring ).
*    lv_node = lv_reader->read_next_node( ).
*    ASSERT lv_node IS NOT INITIAL.
*    WHILE lv_reader->node_type <> if_sxml_node=>co_nt_final.
*      CASE lv_reader->node_type.
*        WHEN if_sxml_node=>co_nt_element_open.
*          lv_open_node  ?= lv_node.
*          out->write('OpenNode: ' && lv_open_node->qname-name ).
*
*          DATA(attrs) = lv_open_node->get_attributes( ).
*          LOOP AT attrs ASSIGNING FIELD-SYMBOL(<attr>).
*            out->write('Attribute: ' && <attr>->qname-name && ' Value: ' && <attr>->get_value( ) ).
*
*          ENDLOOP.
*        WHEN if_sxml_node=>co_nt_element_close.
*          lv_close_node ?= lv_node.
*          out->write('CloseNode: ' && lv_close_node->qname-name ).
*
*        WHEN if_sxml_node=>co_nt_value.
*          lv_value_node ?= lv_node.
*          lv_value = lv_value_node->get_value( ).
*          out->write('ValueNode: ' && lv_value ).
*
*        WHEN if_sxml_node=>co_nt_attribute.
*      ENDCASE.
*      lv_node = lv_reader->read_next_node( ).
*    ENDWHILE.
  ENDMETHOD.
ENDCLASS.
